//Flies by and shoots tiny bullets at the player

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Ghosts.png");
let SEshots4=("script\SoundEffects\shots4.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;
let startx=GetX; let starty=GetY;
let frame=0; let time=0; let animation=0;
let ani=6;
let scale=0.75;
let shots=0;
let shotcolor=3; let shotspeed=0;
let oldX=0; let oldY=0;
let invincible=125;

let direction=0;
if(startx<cx){ SetAngle(45); }
if(startx>=cx){ SetAngle(135); }
SetSpeed(5);

@Initialize{
	LoadGraphic("\script\Images\Enemies\Ghosts.png");
	LoadSE("script\SoundEffects\shots4.wav");

	SetInvincibility(20);
	SetLife(7);
	SetDamageRate(10,3); 
	SetScore(1000);
	SetShotColor(255,255,255);
	SetShotAutoDeleteClip(32,32,32,32);
}

@MainLoop{

if(GetX<=minx-32 || GetX>=maxx+32 || GetY<=miny-32 || GetY>=maxy+32 && frame>=60){ VanishEnemy; }

if((GetX>minx && GetX<maxx) && (GetY>miny && GetY<maxy)){
SetCollisionA(GetX,GetY,32*scale);
SetCollisionB(GetX,GetY,32*scale);


if(GetCommonData("Difficulty")==1){
	if(GetX>minx+20 && GetX<maxx-20 && time%8==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(6){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),2,angle+rand(-15,15),-0.04,rand(1.3,1.5),rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),2,angle+rand(-15,15),-0.04,rand(1.3,1.5),rand_int(6,7),5); }
		angle+=360/6;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%20==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(6){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),2,angle+rand(-18,18),-0.04,rand(1.3,1.5),168+rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),2,angle+rand(-18,18),-0.04,rand(1.3,1.5),168+rand_int(6,7),5); }
		angle+=360/6;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%5==0){ PlaySE(SEshots4); }
} //Easy

if(GetCommonData("Difficulty")==2){
	if(GetX>minx+20 && GetX<maxx-20 && time%6==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(8){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),3,angle+rand(-16,16),-0.04,rand(1.1,1.4),rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),3,angle+rand(-16,16),-0.04,rand(1.1,1.4),rand_int(6,7),5); }
		angle+=360/8;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%20==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(8){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),3,angle+rand(-16,16),-0.04,rand(1.1,1.4),168+rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),3,angle+rand(-16,16),-0.04,rand(1.1,1.4),168+rand_int(6,7),5); }
		angle+=360/8;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%5==0){ PlaySE(SEshots4); }
} //Normal

if(GetCommonData("Difficulty")==3){
	if(GetX>minx+20 && GetX<maxx-20 && time%5==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(10){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),4,angle+rand(-14,14),-0.05,rand(0.9,1.3),rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),4,angle+rand(-14,14),-0.05,rand(0.9,1.3),rand_int(6,7),5); }
		angle+=360/10;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%20==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(10){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),4,angle+rand(-14,14),-0.05,rand(0.9,1.3),168+rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),4,angle+rand(-14,14),-0.05,rand(0.9,1.3),168+rand_int(6,7),5); }
		angle+=360/10;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%5==0){ PlaySE(SEshots4); }
} //Hard

if(GetCommonData("Difficulty")==4){
	if(GetX>minx+20 && GetX<maxx-20 && time%4==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(12){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),5,angle+rand(-12,12),-0.06,rand(0.7,1.2),rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),5,angle+rand(-12,12),-0.06,rand(0.7,1.2),rand_int(6,7),5); }
		angle+=360/12;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%20==0){
	let radius=rand(5,30);
	let angle=GetAngleToPlayer;
		loop(12){
			if(startx<cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),5,angle+rand(-12,12),-0.06,rand(0.7,1.2),168+rand_int(2,3),5); }
			if(startx>cx){ CreateShot02(GetX+radius*cos(angle),GetY+radius*sin(angle),5,angle+rand(-12,12),-0.06,rand(0.7,1.2),168+rand_int(6,7),5); }
		angle+=360/12;
		}
	shots++;
	}
	if(GetX>minx+20 && GetX<maxx-20 && time%5==0){ PlaySE(SEshots4); }
} //Lunatic
} //Onscreen


if(startx<cx && time>=30){ SetAngle(45-direction); }
if(startx>=cx && time>=30){ SetAngle(135+direction); }
if(direction<45 && time>=30 && time<90){ direction+=1; }
if(direction>0 && time>=90){ direction-=1; }
if(time<30){ SetSpeed(GetSpeed-0.05); }
if(time>=80){ SetSpeed(GetSpeed+0.05); }


oldX=GetX;
oldY=GetY;
frame++;
time++;
animation++;
if(animation>=ani*6){ animation=0; }
if(GetTimeOfInvincibility<25 && invincible>0){ invincible=GetTimeOfInvincibility*5; }

}

@DrawLoop{
	SetTexture(GRenemy);
		if(animation>=ani*0 && animation<ani*1){ SetGraphicRect(0,151,44,210); }
		if(animation>=ani*1 && animation<ani*2){ SetGraphicRect(44,151,87,210); }
		if(animation>=ani*2 && animation<ani*3){ SetGraphicRect(88,151,132,210); }
		if(animation>=ani*3 && animation<ani*4){ SetGraphicRect(133,151,176,210); }
		if(animation>=ani*4 && animation<ani*5){ SetGraphicRect(177,151,220,210); }
		if(animation>=ani*5 && animation<ani*6){ SetGraphicRect(221,151,264,210); }
		if(animation>=ani*6 && animation<ani*7){ SetGraphicRect(265,151,308,210); }
	SetGraphicAngle(0,0,0);
	if(startx<cx){ SetColor(255,200,0); }
	if(startx>cx){ SetColor(0,200,255); }
	SetGraphicScale(1,1);
	SetRenderState(ADD);
	DrawGraphic(GetX,GetY);
}

@Finalize{
if(BeVanished==false){
PlaySE(SEdeath);
	CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0);
	CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0);
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

}